$(function() {
	var service = new EbeimService();
	var json = {};
	service.getSystemInfo(json, callback1);
	$(".lab-check").bind("click", function() {
		var t = $(this);
		if (t.hasClass("check-true")) {
			t.removeClass("check-true");
			t.siblings("input[type='checkbox']").prop("checked", false);
		} else {
			t.addClass("check-true");
			t.siblings("input[type='checkbox']").prop("checked", true);
		}
	})
		$("#lock").bind("click", function() {
		var t = $(this);
		if (t.hasClass("check-true")) {
			$("input[name='lockTime']").removeAttr("disabled");
		
		} else {
			$("input[name='lockTime']").attr("disabled", "disabled");
		}
	})
})

var callback1 = function(data) {
	$("input[name='writeLimit']").val(data.writeLimit);
	$("input[name='readLimit']").val(data.readLimit);
	$("input[name='serverIp']").val(data.apiHost + ":" + data.port);
	setLockTime(data.lockTime);
}

var callback = function(data) {
	if (data.code == "200" || data.code == 200) {
		$("input[name='writeLimit']").val(data.writeLimit);
		$("input[name='readLimit']").val(data.readLimit);
		$("input[name='serverIp']").val(data.apiHost + ":" + data.port);
		setLockTime(data.lockTime);
		plugin.msgtip({
			type : "o",
			msg : "设置成功！"
		});
	} else {
		plugin.msgtip({
			type : "e",
			msg : "设置失败！"
		});
	}

}
function validate(str) {
	var reg = new RegExp("^[0-9]*$");
	var obj = document.getElementById("name");
	if (!reg.test(str)) {
		return false;
	}
	return true;
}

function saveSys() {
	var writeLimit = $("input[name='writeLimit']").val();
	var readLimit = $("input[name='readLimit']").val();
	var lockSel = document.getElementById('lockSel');
	var lockTime = $("input[name='lockTime']").val();
	var flag = validate(writeLimit);
	if (!flag) {
		alert("最大上行速度必须输入整数");
		return;
	}
	flag = validate(readLimit);
	if (!flag) {
		alert("最大下行速度必须输入整数");
		return;
	}
	if (lockSel.checked) {
		flag = validate(lockTime);
		if (!flag) {
			alert("锁定时间必须输入整数");
			return;
		}
	}else{
		lockTime=-1;
	}

	var service = new EbeimService();
	var json = {};
	json.writeLimit =writeLimit;
	json.readLimit =readLimit;
	json.lockTime = lockTime;
	service.setSystemInfo(json, callback);
}

function setLockTime(time) {
	var lockSel = $("#lockSel");
	if (time == -1) {
		lockSel.prop("checked", false);
		$("input[name='lockTime']").val("");
		$("input[name='lockTime']").attr("disabled", "disabled");
	} else {
		lockSel.prop("checked", true);
		lockSel.siblings("label").addClass("check-true");
		$("input[name='lockTime']").val(time);
		$("input[name='lockTime']").removeAttr("disabled");
	}

}
